<?php

/*************************************************************************
 *
 *  System Name     ： UI TOOL
 *  Include Name  	： class.file.php
 *  Include IＤ		： file
 *
 *  Writer          ： Minz
 *  Create Date		： 2011/10/20
 *
 *************************************************************************/


class FileClass
{

    /**
     * Make Directory
     * @param $dir_path
     */
	function make_dir($dir_path)
	{
		$ex_path=explode('/',$dir_path);
		foreach($ex_path as $temp_path)
		{
			$make_path .= $temp_path.'/';
			if(strpos($_SERVER['DOCUMENT_ROOT'].'/', $make_path) === false)
			{
				echoo($make_path);
                if(is_dir($make_path) == false) {
                    if(@mkdir($make_path, 0707)) {
                        @chmod($make_path, 0707);
                    } else {
                       echo " Don't create directory -> $make_path";
                       exit(1);
                    }
                }
			}
		}
	}

    /**
     * Make File Name
     * @param 	$file_name => $_FILE[upload][name]
     * 			$pre   => prefix of file name	(ex : "img_" => img_1230149010.jpg )
     */
	function make_name($file_name,$pre="")
	{
		$temp=explode('.',$file_name);
		$ext= $temp[count($temp)-1];
		if(!empty($pre))
		{
			$result = uniqid($pre).".".$ext;
		}else
		{
			$result = uniqid().".".$ext;
		}
		return $result;
	}

    /**
     * Make Thumbnail File Name
     * @param 	$file_name => $_FILE[upload][name]
     * 			$pre   => prefix of file name	(ex : "img_" => img_1230149010.jpg )
     * 			$pre   => prefix of file name	(ex : "img_" => img_1230149010.jpg )
     */
	function make_thumbnail_name($file_name,$pre,$new_pre)
	{
		$result = str_replace($pre, $new_pre, $file_name);
		return $result;
	}


    /**
     * Check File Type
     * @param 	$file_name => $_FILE[upload][name]
     * 			$ini_ext   => accept ext　　(ex : "jpg,bmp,png,gif" => 123.jpg ->ok, 123.php ->no )
     */
	function check_file_type($file_name,$ini_ext)
	{
		$temp=explode('.',$file_name);
		$ext= $temp[count($temp)-1];
        $patten = '/'.$ext.'/i';
        if(!preg_match($patten, $ini_ext)) {
        	return true;
        }else
        {
        	return false;
        }
	}

    /**
     * Check Image Size
     * @param 	$file_name 	=> File full path
     * 			$width   	=> width
     * 			$height   	=> height
     **/
	function check_image_size($file_name, $width, $height)
	{
		if ($getimagesize = getimagesize($file_name))
		{
            if($getimagesize[0] >$width)
            {
				echo"image size over -> width : $getimagesize[0]";
            }
            if($getimagesize[1] > $height)
            {
				echo"image size over -> height : $getimagesize[1]";
            }
        }
	}


    /**
     * Move File
     * @param 	$upload_file => $_FILE[upload]['tmp_name']
     * 			$new_file    => new File path and name (ex : "")
     */
	function move_file($upload_file, $new_file)
	{
		if (!move_uploaded_file($upload_file, $new_file))
		{
			echo "file move error";
		}
	}


    /**
     * Make Thumbnail File
     * @param 	$org_name 	=>	original image file path
     * 			$width		=>	change image width
     * 			$height 	=>	change image height
     * 			$thumb_name	=>	new thumbnamil file path
     */
	function make_thumbnail($org_name, $width, $height, $thumb_name)
	{
		$file_info = @GetImageSize($org_name);

		if(!$file_info[2] || ($file_info[2] < 1 && $file_info[2] > 3)) return false;

		$gd = gd_info();
		$gdver = substr(preg_replace("/[^0-9]/", "", $gd['GD Version']), 0, 1);
		if(!$gdver) return false;

		if($file_info[0] < $width && $file_info[1] < $height) {
			$new_w = $file_info[0];
			$new_h = $file_info[1];
		} else if($file_info[0] > $file_info[1]) {
			$new_w = $width;
			$new_h = ($file_info[1] * $width) / $file_info[0];
		} else if($file_info[0] < $file_info[1]) {
			$new_w = ($file_info[0] * $height) / $file_info[1];
			$new_h = $height;
		} else if ($file_info[0] == $file_info[1]) {
			$new_w = $width;
			$new_h = $height;
		}

		if($file_info[2]==1) $src_img = ImageCreateFromGIF($org_name);
		else if($file_info[2]==2) $src_img = ImageCreateFromJPEG($org_name);
		else $src_img = ImageCreateFromPNG($org_name);


		if($new_w < $width) $srcx = ceil(($width - $new_w)/2);
		else $srcx = 0;
		if($new_h < $height) $srcy = ceil(($height - $new_h)/2);
		else $srcy = 0;

		if($gdver >= 2){
			$dst_img = ImageCreateTrueColor($new_w, $new_h);

			ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, Imagesx($src_img), imagesy($src_img));
		}else{
			$dst_img = ImageCreate($new_w, $new_w);
			ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, Imagesx($src_img), imagesy($src_img));
		}

		if($file_info[2]==1) ImageGIF($dst_img,$thumb_name);
		else if($file_info[2]==2) ImageJPEG($dst_img,$thumb_name);
		else ImagePNG($dst_img,$thumb_name);

		if(is_file($thumb_name)) return true;
		else return false;
	}



    /**
     * Make Thumbnail File
     * @param 	$org_name 	=>	original image file path
     * 			$width		=>	change image width
     */
	function file_download($download_file,$org_name)
	{
		$temp=explode('.',$download_file);
		$temp_name = $temp[count($temp)-1];
		$file_name = ($org_name) ? $org_name : $temp_name;

        if(file_exists($download_file) == true) {
            header('Content-type: file/unknown');
            header('Content-Length: '.filesize($_SERVER['DOCUMENT_ROOT'].$download_file));
            header('Content-Disposition: attachment; filename='.$file_name);
            header('Content-Description: PHP4 Generated Data');
            header('Content-Transfer-Encoding: binary ');
            header('Pragma: no-cache');
            header('Expires: 0');
            $fp = fopen($download_file,"r");
            if (!fpassthru($fp)) {
                fclose($fp);
                clearstatcache($fp);
            }
        } else {
			echo "file not found -> $download_file";
        }
	}
//	function fileDownload($download_file, $file_org_name='') {
//        $copy_file_name = substr($download_file, strrpos($download_file, '/')+1);
//        $file_name = ($file_org_name) ? $file_org_name : $copy_file_name;
//        if(file_exists($_SERVER['DOCUMENT_ROOT'].$download_file) == true) {
//            header('Content-type: file/unknown');
//            header('Content-Length: '.filesize($_SERVER['DOCUMENT_ROOT'].$download_file));
//            header('Content-Disposition: attachment; filename='.$file_name);
//            header('Content-Description: PHP4 Generated Data');
//            header('Content-Transfer-Encoding: binary ');
//            header('Pragma: no-cache');
//            header('Expires: 0');
//            $fp = fopen($_SERVER['DOCUMENT_ROOT'].$download_file,"r");
//            if (!fpassthru($fp)) {
//                fclose($fp);
//                clearstatcache($fp);
//            }
//        } else {
//            jsClass::goBack('해당 파일이 존재 하지 않아 다운로드 할 수 없습니다.');
//        }
//    }

    /**
     * File Delete
     * @param 	$file_path 	=>	delete file path
     */
	function file_delete($file_path)
	{
 		if(file_exists($file_path)) {
            if(unlink($file_path) == false) {
                echo "delete error -> $file_path";
                exit(1);
            }
        }
	}


    /**
     * 파일 업로드
     * @param $files $_FILES
     * @param $option array('path'=>'/FILE_DATA/a', 'ext'=>'jpg:gif', 'upload_size'=>1000, 'img_size'=>'500*500')
     */
    function upload($files, $option) {
        $ini_file_uploads = ini_get('file_uploads');
        if($ini_file_uploads == 0) jsClass::goPage($_SERVER['REQUEST_URI'], '시스템 에러 입니다. php.ini 설정의 file_uploads 값 참고');
        $php_ini_upload_error_msg = '시스템 에러 입니다. php.ini 설정의 upload_max_filesize 값 참고';
        if(is_array($files['name'])) {
            $this->upload_count = count($files['name']);
            for($x=0; $x<$this->upload_count; $x++) {
                if($files['name'][$x]) {
                    if($files['error'][$x] != 0) jsClass::goPage($_SERVER['REQUEST_URI'], $php_ini_upload_error_msg);
                }
            }
        } else {
            if($files['name']) {
                if($files['error'] != 0) jsClass::goPage($_SERVER['REQUEST_URI'], $php_ini_upload_error_msg);
            }
        }
        $this->uploadExtChk($files, $option['ext']);
        $this->uploadMaxSizeChk($files, $option['upload_size']);
        $this->uploadImgSizeChk($files, $option['img_size']);
        $result = $this->uploadCopy($files, $option['path']);
        return $result;
    }



}
if(!is_object( $FileClass ))
{
	$FileClass = new FileClass;
}

?>

